package com.luck.pf.security.tag;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.luck.org.entity.SysMenu;
import com.luck.pf.common.Constants;

/**
 * 说明:
 * Copyright: Copyright (c) 2010
 * @author zhangwr 2011-8-20 下午07:16:43
 * @version 1.0
 */
public class AuthButtonTag extends TagSupport {
	private static final long serialVersionUID = 2137625995532857210L;
	private String authUrl;//最重要，与权限菜单配置的url相同
	private String onclick;//触发的onclick
	private String className;
	private String value;
	private String name;
	private String id;
	
	/* 标签初始方法 */
	public int doStartTag() throws JspTagException{
		try {
			// 获取分页器信息对象
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			String path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
			List<SysMenu> menus = (List<SysMenu>) request.getSession().getAttribute(Constants.SESSION_MENUS);
			JspWriter out = pageContext.getOut();
			StringBuffer bf = new StringBuffer(1000);
			//计算是否具有此权限
			boolean isAuth = checkAuth(authUrl,menus);
			if(isAuth){
				//bf.append("<input type=\"button\" class=\""+className+"\" value=\""+value + "\"name=\""+name +"\" id=\""+id+"\" onclick=\""+onclick+"\" />");
				bf.append("<input type='button' value='"+value+"'");
				if(StringUtils.isNotBlank(className))
					bf.append(" class='"+className+"'");
				if(StringUtils.isNotBlank(name))
					bf.append(" name='"+name+"'");
				if(StringUtils.isNotBlank(id))
					bf.append(" id='"+id+"'");
				if(StringUtils.isNotBlank(onclick)){
					//System.out.println(onclick);
					onclick=onclick.replace("'", "\"");
					onclick = onclick.replace("<%=path%>", path);					
					bf.append(" onclick='"+onclick+"'");
				}
				bf.append(" />");			
			}
			String dd = (String) getValue("path");
			out.write(bf.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	private boolean checkAuth(String authUrl, List<SysMenu> menus) {
		boolean checkAuth = false;
		for(SysMenu menu:menus){
			//如果url不为空则比对
			if(StringUtils.isNotBlank(menu.getUrl())){
				if(filterUrl(authUrl).equals(filterUrl(menu.getUrl()))){
					checkAuth = true;
					break;
				}
			}
		}
		return checkAuth;
	}

	private String filterUrl(String url){
		if(StringUtils.isEmpty(url))
			return url;
		if(url.startsWith("/"))
			url = url.substring(1,url.length());
		int i = url.indexOf("?");
		if(i!=-1)
			url = url.substring(0, i);
		return url;
	}

	/* 标签结束方法 */
	public int doEndTag() throws JspTagException{

	      return super.SKIP_BODY;
	}
	
	
	
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getOnclick() {
		return onclick;
	}
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

	public String getAuthUrl() {
		return authUrl;
	}

	public void setAuthUrl(String authUrl) {
		this.authUrl = authUrl;
	}
}
